package org.red_cross_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.transaction.Transactional;
import org.red_cross_service.utils.EasyExcelUtils;
import org.red_cross_service.utils.QueryWrapperUtils;
import org.red_cross_service.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.red_cross_service.common.response.Response;
import org.red_cross_service.domain.entity.UserEntity;
import org.red_cross_service.mapper.UserMapper;

import java.util.List;

@Service
public class UserService {

    @Autowired
    ServiceComponent serviceComponent;

    @Autowired
    private UserMapper userMapper;


    @Transactional
    public Response login(UserEntity user) {
        //依据openid查找用户 如果用户不存在 则往表里添加
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        UserEntity userEntity = userMapper.selectOne(queryWrapper.eq("open_id", user.getOpenId()));
        if(userEntity == null) { //新用户
            userMapper.insert(user);
            return Response.success("登录成功",user);
        }else{
            return Response.success("登录成功",userEntity);
        }
    }

    public Response getUserAvatar(String idCard) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        UserEntity userEntity = userMapper.selectOne(queryWrapper.eq("id_card", idCard));
        if(userEntity == null) {
            return Response.error("未查询到头像");
        }
        return Response.success("查询成功",userEntity.getAvatarUrl());
    }

    public Response getUserList(UserEntity user) {
        return Response.success(
                userMapper.selectList(
                    QueryWrapperUtils.buildQuery(
                        user
                    )
                )
        );
    }

    public Response updateUser(UserEntity user) {
        return Response.success(
                userMapper.updateById(
                        user
                        )
        );
    }

    public Response getUserOne(int userId) {
        return Response.success(
                userMapper.selectById(
                        userId
                )
        );
    }

    /*
    *  传入openid 返回用户信息
    * */
    public Response autoLogin(String openId) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        UserEntity userEntity = userMapper.selectOne(queryWrapper.eq("open_id", openId));
        if(userEntity == null){
            return Response.success("未查询到信息",null);
        }
        return Response.success("查询成功",userEntity);
    }

    public void exportUser(HttpServletResponse response, UserEntity user) {
        List<UserEntity> userEntitieList = userMapper.selectList(QueryWrapperUtils.buildQuery(user));
        try {
            EasyExcelUtils.writeWeb(response,userEntitieList, UserEntity.class,"用户列表","用户列表");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
